package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.JcyzSdDaqi;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.JcyzSdDaqiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 手动监测因子地下水 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-02-19
 */
@Api(tags = "手动监测因子大气")
@RestController
@RequestMapping("/jcyz-sd-daqi")
public class JcyzSdDaqiController {


    @Autowired
    private JcyzSdDaqiService jcyzSdDaqiService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    public Result getById(String id) {
        return Result.successResult(jcyzSdDaqiService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST")
    public Result save(@RequestBody JcyzSdDaqi jcyzSdDaqi) {
        jcyzSdDaqi.setDelFlag(0);
        jcyzSdDaqiService.save(jcyzSdDaqi);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "通过id删除", notes = "通过id删除", httpMethod = "GET")
    public Result delete(@PathVariable(value = "id") String id) {
        jcyzSdDaqiService.removeById(Long.parseLong(id));
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody JcyzSdDaqi jcyzSdDaqi) {
        jcyzSdDaqiService.updateById(jcyzSdDaqi);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result list(@RequestBody JcyzSdDaqiController.JcyzSdDaqiReqVo jcyzSdDaqi) {
        final LambdaQueryWrapper<JcyzSdDaqi> lambda = new QueryWrapper<JcyzSdDaqi>().lambda();
        this.buildCondition(lambda, jcyzSdDaqi);
        return Result.successResult(jcyzSdDaqiService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "POST")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<JcyzSdDaqi> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        Map<String, Object> condition = pageInfo.getCondition();
        final LambdaQueryWrapper<JcyzSdDaqi> lambda = new QueryWrapper<JcyzSdDaqi>().lambda();
        lambda.eq(JcyzSdDaqi::getDelFlag,0);
        if (condition.get("name")!=null && !StringUtils.isEmpty(condition.get("name").toString())){
            lambda.like(JcyzSdDaqi::getName,condition.get("name"));
        }
        if (condition.get("isGb")!=null && !StringUtils.isEmpty(condition.get("isGb").toString())){
            lambda.like(JcyzSdDaqi::getIsGb,condition.get("isGb"));
        }
        return Result.successResult(this.jcyzSdDaqiService.page(page, lambda));
    }

    /**
     * 获取分类
     */
    @PostMapping("/type/list")
    @ApiOperation(value = "获取分类", notes = "获取分类", httpMethod = "POST")
    public Result typeList() {
        LambdaQueryWrapper<JcyzSdDaqi> lambda = new QueryWrapper<JcyzSdDaqi>().lambda();
        lambda.eq(JcyzSdDaqi::getDelFlag, 0).groupBy(JcyzSdDaqi::getJcType);
        List<JcyzSdDaqi> list = this.jcyzSdDaqiService.list(lambda);
        List<String> type = new ArrayList<>();
        for (JcyzSdDaqi  jcyzSdDaqi: list
        ) {
            type.add(jcyzSdDaqi.getJcType());
        }
        return Result.successResult(type);
    }
    /**
     * 根据类型获取因子
     */
    @GetMapping("/listByType")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jcType", value = "jcType", required = true, paramType = "query", dataType = "String")
    })
    @ApiOperation(value = "根据类型获取因子", notes = "根据类型获取因子", httpMethod = "GET")
    public Result listByType(String jcType) {
        LambdaQueryWrapper<JcyzSdDaqi> lambda = new QueryWrapper<JcyzSdDaqi>().lambda();
        lambda.eq(JcyzSdDaqi::getDelFlag, 0).groupBy(JcyzSdDaqi::getJcType).eq(JcyzSdDaqi::getJcType,jcType);
        List<JcyzSdDaqi> list = this.jcyzSdDaqiService.list(lambda);
        return Result.successResult(list);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<JcyzSdDaqi> lambda, JcyzSdDaqiReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(JcyzSdDaqi::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(JcyzSdDaqi::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getType())) {
            lambda.eq(JcyzSdDaqi::getType, param.getType());
        }
        if (!StringUtils.isEmpty(param.getJcType())) {
            lambda.eq(JcyzSdDaqi::getJcType, param.getJcType());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(JcyzSdDaqi::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(JcyzSdDaqi::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(JcyzSdDaqi::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(JcyzSdDaqi::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(JcyzSdDaqi::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(JcyzSdDaqi::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(JcyzSdDaqi::getDelFlag, param.getDelFlag());
        }
        if (!StringUtils.isEmpty(param.getNo())) {
            lambda.eq(JcyzSdDaqi::getNo, param.getNo());
        }
        if (!StringUtils.isEmpty(param.getUnitMeasurement())) {
            lambda.eq(JcyzSdDaqi::getUnitMeasurement, param.getUnitMeasurement());
        }
        if (!StringUtils.isEmpty(param.getIsGb())) {
            lambda.eq(JcyzSdDaqi::getIsGb, param.getIsGb());
        }
        if (!StringUtils.isEmpty(param.getValue())) {
            lambda.eq(JcyzSdDaqi::getValue, param.getValue());
        }
        lambda.orderBy(true, false, JcyzSdDaqi::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class JcyzSdDaqiReqVo extends JcyzSdDaqi {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
